<?php

/**
 *
 */
class ZhaopinController extends Common
{
    public $sima;
    public $member;
    public $pagesize = 7;

    function __construct()
    {
        parent::__construct();
        $this->member = $this->model('member');
        $this->sima = $this->model('zhou_sima');
    }

    //招聘列表
    public function jobs_listAction()
    {
        $daoqi = $this->member->get_all_daoqi("endtime < ".time());
        if(count($daoqi)>0){
            foreach($daoqi as $key => $value){
                $data['is_daoqi'] = 1;
                $this->member->set($value['id'],$data);
            }
        }
        if(!get_cookie('openid')){
		    $this->oauthlogin();//微信授权跟获取
		}
        //区域
        $area = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =0');

        $data = array();
        foreach ($area as $key => $value) {
            $data[$key]['id'] = $value['area_id'];
            $data[$key]['name'] = $value['area_name'];
            $data[$key]['child'] = array();
            $area_a = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =' . $value['area_id']);
            if ($area_a) {
                foreach ($area_a as $key2 => $value2) {
                    $data[$key]['child'][$key2]['id'] = $value2['area_id'];
                    $data[$key]['child'][$key2]['name'] = $value2['area_name'];
                    $data[$key]['child'][$key2]['child'] = array();
                    $area_b = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =' . $value2['area_id']);
                    foreach ($area_b as $key3 => $value3) {
                        $data[$key]['child'][$key2]['child'][$key3]['id'] = $value3['area_id'];
                        $data[$key]['child'][$key2]['child'][$key3]['name'] = $value3['area_name'];
                    }
                }
            }
        }

        //轮播图
        $pic = $this->sima->get_one('zhou_pic', 'pic_id = 1');


        $typeid = $this->safe('typeid');
        $position_id = $this->safe('position_id');
        $work_id = $this->safe('work_id');
        $area_id = $this->safe('area_id');
        $search_id = $this->safe('search_id');
        $page = (int)$this->get('page') ? (int)$this->get('page') : 1;
        $where = "1=1 and typeid = $typeid";
        $myurl = "&typeid=$typeid";
        $order = "";
        $limit = ($page - 1) * $this->pagesize . "," . $this->pagesize;
        if(!$typeid){
            echo "<script>alert('操作错误，请重新操作！');location.href='index.php?c=zhou_jobs&a=jobs_list&typeid=1';</script>";die;
        }
        if($position_id){
            $where .= " and jobs_position like '%$position_id%'";
            $myurl .= "&position_id=$position_id";
        }
        if($work_id){
            $where .= " and jobs_work_type = $work_id";
            $myurl .= "&work_id=$work_id";
        }
        if($area_id){
            $where .= " and jobs_area like '%$area_id%'";
            $myurl .= "&area_id=$area_id";
        }
        if($search_id==1){
            $order = "jobs_addtime desc";
        }else if($search_id==2){
            $order = "jobs_host desc";
        }else{
            $order = "jobs_id desc";
        }
        $urlparam = array('page' => '{page}','position'=>$position_id,'work_id'=>$work_id,'area_id'=>$area_id,'search_id'=>$search_id,'typeid'=>$typeid);
        $result = $this->sima->get_all_list($where,$order,$limit);
        $total = $this->sima->get_all_list($where,$order,$limit,true);
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagelist->loadconfig();
        $pagelist = $pagelist->total($total)->url(url('zhou_jobs/jobs_list', $urlparam))->num($this->pagesize)->page($page)->output();

        $position_list = $this->sima->get_cate_all('zhou_position', '1=1 order by position_id asc');
        $position_arr = tran_one($position_list,'position_id','position_name');

        $work = $this->sima->get_cate_all('zhou_work_type', '1=1 order by work_id asc');
        $work_arr = tran_one($work,'work_id','work_name');

        $xinzi = $this->sima->get_cate_all('zhou_salary', '1=1 order by salary_ord asc');
        $xinzi_arr = tran_one($xinzi,'salary_id','salary_name');
        $shoufei = $this->site['SITE_SHOUFEI'];
        $jiage = $this->site['SITE_JIAGE'];
//        set_cookie('openid','owR3l02o7I_IcxX-0ipQ2T4LH0PY',36000);
        $num = $this->member->get_one_openid(get_cookie('openid'));


        $this->view->assign(array(
            'num'=>$num,
            'shoufei'=>$shoufei,
            'jiage'=>$jiage,
            'myurl'=>$myurl,
            'typeid'=>$typeid,
            'position_id'=>$position_id,
            'work_id'=>$work_id,
            'area_id'=>$area_id,
            'search_id'=>$search_id,
            'position_arr'=>$position_arr,
            'work_arr'=>$work_arr,
            'xinzi_arr'=>$xinzi_arr,
            'result'=>$result,
            'position_list' => $position_list,
            'arr' => json_encode($data),
            'pic' => $pic,
            'work' => $work,
            'meta_title' => '招聘列表'));
        $this->view->display('zhaopin/jobs_list');
    }

    //招聘列表
    public function ajax_jobs_listAction()
    {

        //区域
        $area = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =0');

        $data = array();
        foreach ($area as $key => $value) {
            $data[$key]['id'] = $value['area_id'];
            $data[$key]['name'] = $value['area_name'];
            $data[$key]['child'] = array();
            $area_a = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =' . $value['area_id']);
            if ($area_a) {
                foreach ($area_a as $key2 => $value2) {
                    $data[$key]['child'][$key2]['id'] = $value2['area_id'];
                    $data[$key]['child'][$key2]['name'] = $value2['area_name'];
                    $data[$key]['child'][$key2]['child'] = array();
                    $area_b = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =' . $value2['area_id']);
                    foreach ($area_b as $key3 => $value3) {
                        $data[$key]['child'][$key2]['child'][$key3]['id'] = $value3['area_id'];
                        $data[$key]['child'][$key2]['child'][$key3]['name'] = $value3['area_name'];
                    }
                }
            }
        }
        //轮播图
        $pic = $this->sima->get_one('zhou_pic', 'pic_id = 1');


        $typeid = $this->safe('typeid');
        $position_id = $this->safe('position_id');
        $work_id = $this->safe('work_id');
        $area_id = $this->safe('area_id');
        $search_id = $this->safe('search_id');
        $page = (int)$this->get('page') ? (int)$this->get('page') : 1;
        $where = "1=1 and typeid = $typeid";
        $myurl = "&typeid=$typeid";
        $order = "";
        $limit = ($page - 1) * $this->pagesize . "," . $this->pagesize;
        if(!$typeid){
            echo "<script>alert('操作错误，请重新操作！');location.href='index.php?c=zhou_jobs&a=jobs_list&typeid=1';</script>";die;
        }
        if($position_id){
            $where .= " and jobs_position like '%$position_id%'";
            $myurl .= "&position_id=$position_id";
        }
        if($work_id){
            $where .= " and jobs_work_type = $work_id";
            $myurl .= "&work_id=$work_id";
        }
        if($area_id){
            $where .= " and jobs_area like '%$area_id%'";
            $myurl .= "&area_id=$area_id";
        }
        if($search_id==1){
            $order = "jobs_addtime desc";
        }else if($search_id==2){
            $order = "jobs_host desc";
        }else{
            $order = "jobs_id desc";
        }
        $urlparam = array('page' => '{page}','position'=>$position_id,'work_id'=>$work_id,'area_id'=>$area_id,'search_id'=>$search_id,'typeid'=>$typeid);
        $result = $this->sima->get_all_list($where,$order,$limit);
        $total = $this->sima->get_all_list($where,$order,$limit,true);
        $pagelist = $this->instance('pagelist');    //加载分页类
        $pagelist->loadconfig();
        $pagelist = $pagelist->total($total)->url(url('zhou_jobs/jobs_list', $urlparam))->num($this->pagesize)->page($page)->output();

        $position_list = $this->sima->get_cate_all('zhou_position', '1=1 order by position_id asc');
        $position_arr = tran_one($position_list,'position_id','position_name');

        $work = $this->sima->get_cate_all('zhou_work_type', '1=1 order by work_id asc');
        $work_arr = tran_one($work,'work_id','work_name');

        $xinzi = $this->sima->get_cate_all('zhou_salary', '1=1 order by salary_ord asc');
        $xinzi_arr = tran_one($xinzi,'salary_id','salary_name');
        $shoufei = $this->site['SITE_SHOUFEI'];
        $jiage = $this->site['SITE_JIAGE'];
//        set_cookie('openid','6666',36000);
        $num = $this->member->get_one_openid(get_cookie('openid'));
        $this->view->assign(array(
            'arr' => json_encode($data),
            'num'=>$num,
            'shoufei'=>$shoufei,
            'jiage'=>$jiage,
            'myurl'=>$myurl,
            'typeid'=>$typeid,
            'position_id'=>$position_id,
            'work_id'=>$work_id,
            'area_id'=>$area_id,
            'search_id'=>$search_id,
            'position_arr'=>$position_arr,
            'work_arr'=>$work_arr,
            'xinzi_arr'=>$xinzi_arr,
            'result'=>$result,
            'position_list' => $position_list,
            'pic' => $pic,
            'work' => $work,
            'meta_title' => '招聘列表'));
        $pagenum = ceil($total / $this->pagesize);
        if ($page > $pagenum) {
            echo 1;
            exit;
        } else {
            $this->view->display('zhaopin/ajax_jobs_list');
        }
    }

    //求职发布
    public function jobs_editAction()
    {
        if ($_POST) {
            $data = array();
            $data['jobs_name'] = $_POST['name'];
            $data['jobs_nikename'] = $_POST['nikename'];
            $data['jobs_content'] = $_POST['content'];
            $data['jobs_area'] = $_POST['area'];
            $data['jobs_tel'] = $_POST['phone'];
            $data['jobs_position'] = implode(',', $_POST['position']);
            $data['jobs_salary'] = $_POST['salary'];
            $data['jobs_work_time'] = $_POST['salary'];
            $data['jobs_work_type'] = $_POST['work_id'];
            $data['typeid'] = 2;
            $data['jobs_addtime'] = time();
            $res = $this->sima->db_add('zhou_jobs', $data);
            if ($res) {
                echo '<script>alert("发布成功！");location.href="index.php?c=zhaopin&a=jobs_list&typeid=2"</script>';
            }
        }
        //薪资范围
        $salary = $this->sima->get_cate_all('zhou_salary', ' 1 order by salary_ord asc');
        //从业时间
        $work_time = $this->sima->get_cate_all('zhou_work_time', 'time_show = 1 and 1 order by time_ord asc');
        //区域
        $area = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =0');
        $data = array();
        foreach ($area as $key => $value) {
            $data[$key]['id'] = $value['area_id'];
            $data[$key]['name'] = $value['area_name'];
            $data[$key]['child'] = array();
            $area_a = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =' . $value['area_id']);
            if ($area_a) {
                foreach ($area_a as $key2 => $value2) {
                    $data[$key]['child'][$key2]['id'] = $value2['area_id'];
                    $data[$key]['child'][$key2]['name'] = $value2['area_name'];
                    $data[$key]['child'][$key2]['child'] = array();
                    $area_b = $this->sima->get_cate_all('zhou_area', 'area_show = 1 and area_p_id =' . $value2['area_id']);
                    foreach ($area_b as $key3 => $value3) {
                        $data[$key]['child'][$key2]['child'][$key3]['id'] = $value3['area_id'];
                        $data[$key]['child'][$key2]['child'][$key3]['name'] = $value3['area_name'];
                    }
                }
            }
        }
        //职位
        $position_list = $this->sima->get_cate_all('zhou_position', '1=1 order by position_id asc');
        //工种
        $work = $this->sima->get_cate_all('zhou_work_type',
            '1=1 order by work_id asc');
        $this->view->assign(array(
              'position_list' => $position_list,
              'work_time' => $work_time,
              'salary' => $salary,
              'arr' => json_encode($data),
              'work' => $work,
              'meta_title' => '发布招聘信息',));
        $this->view->display('zhaopin/jobs_edit');
    }

    public function detailAction(){
        $id = $this->safe('id');
        if($id){
            $list = $this->sima->get_all_show($id);
            $xinzi = $this->sima->get_cate_all('zhou_salary', '1=1 order by salary_ord asc');
            $xinzi_arr = tran_one($xinzi,'salary_id','salary_name');
            $position_list = $this->sima->get_cate_all('zhou_position', '1=1 order by position_id asc');
            $position_arr = tran_one($position_list,'position_id','position_name');
            $this->sima->update_hots($id);
        }else{
            echo "<script>alert('操作错误');history.go(-1) ;</script>";
        }

        $this->view->assign(array(
            'position_arr'=>$position_arr,
            'xinzi_arr'=>$xinzi_arr,
            'list'=>$list,
            'meta_title'=>'招聘公司信息',
        ));
        $this->view->display('zhaopin/jobs_detail_need');
    }
}